package com.example.framework.mqtt.core.utils;

import com.example.framework.mqtt.core.properties.MqttConfigProperties;
import com.example.framework.mqtt.core.properties.SSLProperties;
import com.example.framework.mqtt.core.properties.WillMessage;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.codec.MqttVersion;
import net.dreamlu.iot.mqtt.core.client.IMqttClientConnectListener;
import net.dreamlu.iot.mqtt.core.client.MqttClient;
import net.dreamlu.iot.mqtt.core.client.MqttClientCreator;
import net.dreamlu.iot.mqtt.core.client.MqttWillMessage;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  get and set mqtt properties
 * </p>
 *
 * @author sfjiang
 * @since 2024/11/26 17:40
 */
public class MqttClientUtil {

    public static MqttClientCreator initMqttClient(MqttConfigProperties mqttConfigProperties, IMqttClientConnectListener clientConnectListener) {
        MqttClientCreator mqttClientCreator = MqttClient.create()
                .version(MqttVersion.MQTT_5)
                .ip(mqttConfigProperties.getServerIp())
                .port(mqttConfigProperties.getServerPort())
                .clientId(mqttConfigProperties.getClientId())
                .username(mqttConfigProperties.getUsername())
                .password(mqttConfigProperties.getPassword())
                .cleanSession(mqttConfigProperties.isCleanSession())
                .readBufferSize(mqttConfigProperties.getReadBufferSize())
                .maxBytesInMessage(mqttConfigProperties.getMaxByteInMessage())
                .keepAliveSecs(mqttConfigProperties.getKeepAliveInterval())
                .timeout(mqttConfigProperties.getConnectionTimeout())
                .reconnect(mqttConfigProperties.isReconnect())
                .reInterval(mqttConfigProperties.getReconnectInterval())
                .connectListener(clientConnectListener);
        //ssl设置
        sslConfig(mqttConfigProperties, mqttClientCreator);
        //遗嘱消息设置
        willMessageConfig(mqttConfigProperties, mqttClientCreator);
        return mqttClientCreator;
    }

    private static void willMessageConfig(MqttConfigProperties mqttConfigProperties, MqttClientCreator mqttClientCreator) {
        if (mqttConfigProperties.getWillMessage() != null) {
            WillMessage willMessage = mqttConfigProperties.getWillMessage();
            MqttWillMessage.Builder willMessageBuilder = MqttWillMessage.builder();
            willMessageBuilder.topic(willMessage.getTopic())
                    .message(willMessage.getMessage().getBytes(StandardCharsets.UTF_8))
                    .qos(MqttQoS.valueOf(willMessage.getQos()))
                    .retain(mqttConfigProperties.getWillMessage().getRetained());
            mqttClientCreator.willMessage(willMessageBuilder.build());
        }
    }

    private static void sslConfig(MqttConfigProperties mqttConfigProperties, MqttClientCreator mqttClientCreator) {
        SSLProperties ssl = mqttConfigProperties.getSsl();
        if (ssl != null && ssl.isEnabled()) {
            if (ssl.isTwoWay()) {
                mqttClientCreator.useSsl(ssl.getClientKeystorePath(), ssl.getClientKeystorePass(), ssl.getServerTruststorePath(), ssl.getServerTruststorePass());
            } else {
                mqttClientCreator.useSsl(ssl.getServerTruststorePath(), ssl.getServerTruststorePass());
            }
        }
    }
}
